package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.dto.SysUserDTO;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/system/user")
public class SysUserController {

    @Autowired
    private ISysUserService sysUserService;

    @GetMapping("/list")
    public TableDataInfo list(SysUserDTO sysUserDTO)
    {
        LambdaQueryWrapper<SysUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (sysUserDTO.getUserName() != null) {
            lambdaQueryWrapper.like(SysUser::getUserName, sysUserDTO.getUserName());
        }
        if (sysUserDTO.getPhonenumber() != null) {
            lambdaQueryWrapper.eq(SysUser::getPhonenumber, sysUserDTO.getPhonenumber());
        }
        if (sysUserDTO.getStatus() != null) {
            lambdaQueryWrapper.eq(SysUser::getStatus, sysUserDTO.getStatus());
        }

        Page<SysUser> page = new Page<>(sysUserDTO.getPageNum(), sysUserDTO.getPageSize());
        List<SysUser> list = sysUserService.list(page, lambdaQueryWrapper);

        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(200);
        tableDataInfo.setMsg("查询成功");
        tableDataInfo.setRows(list);
        tableDataInfo.setTotal(page.getTotal());
        return tableDataInfo;
    }

    @GetMapping(value = { "/", "/{userId}" })
    public AjaxResult getInfo(@PathVariable(value = "userId", required = false) Long userId)
    {
        AjaxResult ajax = AjaxResult.success();
        if (userId != null)
        {
            SysUser sysUser = sysUserService.getById(userId);
            ajax.put(AjaxResult.DATA_TAG, sysUser);
            ajax.put("postIds", new ArrayList<>());
            ajax.put("roleIds", new ArrayList<>());
        }
        ajax.put("roles", new ArrayList<>());
        ajax.put("posts", new ArrayList<>());
        return ajax;
    }

    @PostMapping
    public AjaxResult add(@RequestBody SysUser user)
    {
        return toAjax(sysUserService.save(user));
    }

    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysUser user)
    {
        return toAjax(sysUserService.updateById(user));
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds)
    {
        return toAjax(sysUserService.removeByIds(Arrays.asList(userIds)));
    }

    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody SysUser user)
    {
        return toAjax(sysUserService.updateById(user));
    }
    protected AjaxResult toAjax(boolean status)
    {
        return status ? AjaxResult.success() : AjaxResult.error();
    }
}
